//
//  BtabbarViewController.m
//  Ten
//
//  Created by anfuer on 2018/11/14.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "BtabbarViewController.h"
#import "Bnav1Controller.h"
#import "Bnav2Controller.h"
#import "Bnav3Controller.h"
#import "Bnav4Controller.h"
#import "KBTabbar.h"

@interface BtabbarViewController ()<UITabBarControllerDelegate>
@property (nonatomic,assign) NSInteger  indexFlag;
@end

@implementation BtabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"BHome" bundle:nil];
    UIStoryboard *mainStoryBoard2 = [UIStoryboard storyboardWithName:@"Bzhiwei" bundle:nil];
    UIStoryboard *mainStoryBoard3 = [UIStoryboard storyboardWithName:@"Bmsg" bundle:nil];
    UIStoryboard *mainStoryBoard4 = [UIStoryboard storyboardWithName:@"Bmy" bundle:nil];
     Bnav1Controller*one = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Bnav1"];
     Bnav2Controller*two = [mainStoryBoard2 instantiateViewControllerWithIdentifier:@"Bnav2"];
     Bnav3Controller*san = [mainStoryBoard3 instantiateViewControllerWithIdentifier:@"Bnav3"];
     Bnav4Controller*si = [mainStoryBoard4 instantiateViewControllerWithIdentifier:@"Bnav4"];
    
    one.tabBarItem.image = [[UIImage imageNamed:@"首页未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    one.tabBarItem.selectedImage = [[UIImage imageNamed:@"首页选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    two.tabBarItem.image = [[UIImage imageNamed:@"薪资未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    two.tabBarItem.selectedImage = [[UIImage imageNamed:@"薪资选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    san.tabBarItem.image = [[UIImage imageNamed:@"消息未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    san.tabBarItem.selectedImage = [[UIImage imageNamed:@"消息选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    si.tabBarItem.image = [[UIImage imageNamed:@"我的未选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    si.tabBarItem.selectedImage = [[UIImage imageNamed:@"我的选中"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    one.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
    two.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
    san.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);
    si.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 0, -8, 0);

    NSArray *item = [NSArray arrayWithObjects:one,two,san,si,nil];
    [self setViewControllers:item];
    
    KBTabbar * tabbar = [[KBTabbar alloc]init];
    [tabbar.centerBtn addTarget:self action:@selector(centerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self setValue:tabbar forKeyPath:@"tabBar"];
    // Do any additional setup after loading the view.
}
- (void)centerBtnClick:(UIButton *)btn{
    self.selectedIndex=1;
//    [MyUtils collviewSelidex:@"发布职位" toVc:self andShopInfo:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          [[NSNotificationCenter defaultCenter] postNotificationName:@"发布职位" object:nil];
    });

}


-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    if (index != self.indexFlag) {
        //执行动画
        NSMutableArray *arry = [NSMutableArray array];
        for (UIView *btn in self.tabBar.subviews) {
            if ([btn isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
                [arry addObject:btn];
            }
        }
        //添加动画
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
        //速度控制函数，控制动画运行的节奏
        animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        animation.duration = 0.2;       //执行时间
        animation.repeatCount = 1;      //执行次数
        animation.autoreverses = YES;    //完成动画后会回到执行动画之前的状态
        animation.fromValue = [NSNumber numberWithFloat:0.7];   //初始伸缩倍数
        animation.toValue = [NSNumber numberWithFloat:1.3];     //结束伸缩倍数
        [[arry[index] layer] addAnimation:animation forKey:nil];
        self.indexFlag = index;
    }
}
@end
